package org.ly2048.com.ly2048.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Point;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.GridLayout;
import android.widget.TextView;

import org.ly2048.com.ly2048.R;

/**
 * Created by liuyong on 2017/7/24.
 */

@SuppressLint("AppCompatCustomView")
public class Item2048 extends TextView {

    private int num;//数字值
    private int bg_color;//背景颜色
    private Point point;
    private GridLayout.LayoutParams lp;

    public Item2048(Context context) {
        super(context);
        lp = new GridLayout.LayoutParams();
//        lp.setGravity(Gravity.CENTER);
        this.setGravity(Gravity.CENTER);
        this.setLayoutParams(lp);
    }

    public void setNum(int num) {
        this.num = num;
        switch (num) {
            case 0:
                bg_color = R.color.whrite;
                break;
            case 2:
                bg_color = R.color.bg_2;
                break;
            case 4:
                bg_color = R.color.bg_4;
                break;
            case 8:
                bg_color = R.color.bg_8;
                break;
            case 16:
                bg_color = R.color.bg_16;
                break;
            case 32:
                bg_color = R.color.bg_32;
                break;
            case 64:
                bg_color = R.color.bg_64;
                break;
            case 128:
                bg_color = R.color.bg_128;
                break;
            case 256:
                bg_color = R.color.bg_256;
                break;
            case 512:
                bg_color = R.color.bg_512;
                break;
            case 1024:
                bg_color = R.color.bg_1024;
                break;
            case 2048:
                bg_color = R.color.bg_2048;
                break;
            case 4096:
                bg_color = R.color.bg_4069;
                break;
            case 8192:
                bg_color = R.color.bg_8192;
                break;
            case 16384:
                bg_color = R.color.bg_16384;
                break;
            default:
                break;
        }
        if (num == 0) {
            setText("");
        } else {
            setText(String.valueOf(num));
        }
        this.setBackgroundResource(bg_color);
    }

    public Point getPoint() {
        return point;
    }

    public int getNum() {
        return num;
    }

    public void setPoint(int row, int clum) {
        if (this.point == null) {
            this.point = new Point();
        }
        this.point.x = clum;
        this.point.y = row;
        lp.columnSpec = GridLayout.spec(clum);
        lp.rowSpec = GridLayout.spec(row);
    }

    public void setSize(int size, int divider, int count) {
        lp.width = size;
        lp.height = size;
        int helfDivider = (int) Math.ceil(divider / 2f);

        if (point.x == 0) {
            lp.leftMargin = divider;
            lp.rightMargin = helfDivider;
        } else if (point.x == count - 1) {
            lp.leftMargin = helfDivider;
            lp.rightMargin = divider;
        } else {
            lp.leftMargin = helfDivider;
            lp.rightMargin = helfDivider;
        }
        if (point.y == 0) {
            lp.topMargin = divider;
            lp.bottomMargin = helfDivider;
        } else if (point.y == count - 1) {
            lp.topMargin = helfDivider;
            lp.bottomMargin = divider;
        } else {
            lp.topMargin = helfDivider;
            lp.bottomMargin = helfDivider;
        }
        setTextSize(TypedValue.COMPLEX_UNIT_PX, size / 3);
    }

}
